
package logins;

import java.security.MessageDigest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author gjun
 */
public class LoginProcess {
    private String account;
    private String password;
    private String url;
    
    public LoginProcess() {
    }
    
    public String execute() throws Exception {
        
        HttpServletRequest request=ServletActionContext.getRequest();
        HttpSession session=request.getSession(true);
        
        String result="fail";
        
        //雜湊密碼
        MessageDigest md = MessageDigest.getInstance("SHA-256");        
        byte[] bb=md.digest(password.getBytes());
        
        db.NWDB mydb=new db.NWDB();
        if(mydb.vaildAccountLogin(account, bb))            
        {
            int memberID = mydb.getAcccountID(account, bb);
            session.setAttribute("MemberID",memberID);
            session.setAttribute("LoginName", account.toLowerCase());
            
            if(session.getAttribute("url")!=null)
            {
                this.url=session.getAttribute("url").toString();
                session.removeAttribute("url");
                result="redirect";
            }
            else
                result="success";
        }
        mydb.dbClose();
        
        return result;

    }

    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
     public String getUrl() {
        return url;
    }
}
